package com.ruoyi.web.controller.demo;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.GuaranteeInfo;
import com.ruoyi.service.GuaranteeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 保函Controller
 *
 * @author lcd
 * @date 2021-07-27
 */
@RestController
@RequestMapping("/basic/guaranteeInfo")
public class GuaranteeInfoController extends BaseController
{
    @Autowired
    private GuaranteeInfoService guaranteeInfoService;

    /**
     * 查询保函列表
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(GuaranteeInfo guaranteeInfo)
    {
        startPage();
        List<GuaranteeInfo> list = guaranteeInfoService.selectGuaranteeInfoList(guaranteeInfo);
        return getDataTable(list);
    }

    /**
     * 导出保函列表
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:export')")
    @Log(title = "保函", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GuaranteeInfo guaranteeInfo)
    {
        List<GuaranteeInfo> list = guaranteeInfoService.selectGuaranteeInfoList(guaranteeInfo);
        ExcelUtil<GuaranteeInfo> util = new ExcelUtil<GuaranteeInfo>(GuaranteeInfo.class);
        return util.exportExcel(list, "保函数据");
    }

    /**
     * 获取保函详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:query')")
    @GetMapping(value = "/{guaWorkId}")
    public AjaxResult getInfo(@PathVariable("guaWorkId") Long guaWorkId)
    {
        return AjaxResult.success(guaranteeInfoService.getById(guaWorkId));
    }

    /**
     * 新增保函
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:add')")
    @Log(title = "保函", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuaranteeInfo guaranteeInfo)
    {
        return toAjax(guaranteeInfoService.save(guaranteeInfo));
    }

    /**
     * 修改保函
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:edit')")
    @Log(title = "保函", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GuaranteeInfo guaranteeInfo)
    {
        return toAjax(guaranteeInfoService.updateById(guaranteeInfo));
    }

    /**
     * 删除保函
     */
    @PreAuthorize("@ss.hasPermi('basic:guaranteeInfo:remove')")
    @Log(title = "保函", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guaWorkIds}")
    public AjaxResult remove(@PathVariable Long[] guaWorkIds)
    {
        List<String> idList = Arrays.stream(guaWorkIds).map(String::valueOf).collect(Collectors.toList());
        return toAjax(guaranteeInfoService.removeByIds(idList));
    }
}